package com.example.demo.dao.warehouse;



import com.example.demo.model.warehouse.Warehouse;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public interface WarehouseDAO {
    //    一对一查询全部
    @Select("select * from warehouse")
    @Result(column = "product_id", property = "product",
            one = @One(select = "com.example.demo.dao.warehouse.ProductDAO.getByProductId",fetchType = FetchType.LAZY))
    @Result(column = "provider_id", property = "provider",
            one = @One(select = "com.example.demo.dao.people.ProviderDAO.getByProviderId",fetchType = FetchType.LAZY))
    public List<Warehouse> selectAll();

    //    添加数据
    @Insert("insert into warehouse(product_id,product_number)values(#{product_id},#{product_number})")
    Integer addWarehouse(Warehouse warehouse);

    //删除数据
    @Delete("delete from warehouse where warehouse_id=#{warehouse_id}")
    Integer deleteById(Integer warehouse_id);

    //    修改数据
    @Update("update warehouse set product_id=#{product_id},product_number=#{product_number} where warehouse_id=#{warehouse_id}")
    Integer updateById(Warehouse warehouse);

    //根据product_id减少库存量
    @Update("update warehouse set product_number=product_number-#{number} where product_id=#{product_id}")
    Integer updateProductNumberById(Integer number, Integer product_id);

    //根据product_id增肌库存量
    @Update("update warehouse set product_number=product_number+#{number} where product_id=#{product_id}")
    Integer AddProductNumberById(Integer number, Integer product_id);
    //  模糊查询
    @Select("<script>"+
            "select * from `warehouse` where" +
            "<if test=\"provider_id !=null and provider_id != ''\">"+
            "provider_id=#{provider_id}"+
            "</if>"+
            "<if test=\"product_id !=null and product_id != ''\">"+
            "and product_id=#{product_id} "+
            "</if>"+
            "</script>")
    @Result(column = "product_id", property = "product",
            one = @One(select = "com.example.demo.dao.warehouse.ProductDAO.getByProductId",fetchType = FetchType.LAZY))
    @Result(column = "provider_id", property = "provider",
            one = @One(select = "com.example.demo.dao.people.ProviderDAO.getByProviderId",fetchType = FetchType.LAZY))
    public List<Warehouse> findWarehouse(Integer product_id,Integer provider_id) ;

}